package br.com.mtg.movethegarbage.screens;

import android.graphics.RectF;
import android.util.Log;
import br.com.mtg.movethegarbage.framework.Game;
import br.com.mtg.movethegarbage.framework.GameTouchEvent;
import br.com.mtg.movethegarbage.framework.Input;
import br.com.mtg.movethegarbage.framework.Screen;
import br.com.mtg.movethegarbage.game.Assets;
import br.com.mtg.movethegarbage.game.R;
import br.com.mtg.movethegarbage.graphics.Camera2D;
import br.com.mtg.movethegarbage.graphics.SpriteManager;
import br.com.mtg.movethegarbage.impl.Button;
import br.com.mtg.movethegarbage.impl.ToggleButton;

public class MainMenuScreen extends Screen
{

	Camera2D cam;
	Button buttonStart, buttonQuit, buttonInfo;
	Button buttonExtra;
	ToggleButton toggleSound;
	SpriteManager spriteManager;
	byte cheat = 0;

	public MainMenuScreen(Game _game)
	{
		super(_game);
		cam = new Camera2D(game.getGraphics());

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();

		spriteManager = new SpriteManager(game.getGraphics(), 1);

		Assets.font.setScale(1.5F * multiplier);
		float stringWidth = Assets.font.getTextLength(game.getStringFromResource(R.string.play)) + 20;
		float stringHeight = Assets.font.getTextHeight();

		buttonStart = new Button(width / 2 - stringWidth / 2, height / 2 - (stringHeight / 2 + 50 * multiplier), game.getStringFromResource(R.string.play),
				1.5F * multiplier, Assets.buttonBackgroundRect);
		buttonQuit = new Button(width - (50 * multiplier + 10), height - 050 * multiplier - 15, 50 * multiplier, 50 * multiplier);
		buttonExtra = new Button(width - (50 * multiplier + 10), height - 100* multiplier - 20, 50 * multiplier, 50 * multiplier);
		buttonInfo = new Button(width - (50 * multiplier + 10), height - 150 * multiplier - 30, 50 * multiplier, 50 * multiplier);
		toggleSound = new ToggleButton(10, 10, 40 * multiplier, 40 * multiplier, Assets.buttonSoundOn, Assets.buttonSoundOff);
		toggleSound.setActive(game.getAudio().soundEnabled);
		buttonQuit.setButtonSprite(Assets.buttonBack);
		buttonExtra.setButtonSprite(Assets.buttonExtra);
		buttonInfo.setButtonSprite(Assets.buttonInfo);
	}

	@Override
	public void update(long delta)
	{
		Input input = game.getInput();
		RectF r = new RectF((width - 10) / 2, (height + 10) / 2, (width + 10) / 2, (height - 10) / 2);
		r.sort();
		synchronized (input)
		{
			for (GameTouchEvent e : input.getTouchEvents())
			{
				if (e.type == GameTouchEvent.TOUCH_DOWN)
				{
					if (r.contains(e.touchX, e.touchY))
						cheat++;
					else
						cheat = 0;
				}
				buttonStart.handleEvent(e, game);
				buttonQuit.handleEvent(e, game);
				buttonExtra.handleEvent(e, game);
				buttonInfo.handleEvent(e, game);
				toggleSound.handleEvent(e, game);
			}
			input.getTouchEvents().clear();
			input.getKeyEvents().clear();
		}
		if (cheat == 5)
		{
			try
			{
				game.saveProgress(20);
			}
			catch (Exception e)
			{
				Log.e("Cheat", "Sad, no cheat =(");
			}
		}
		if (buttonQuit.wasPressed() || input.wasBackPressed())
			game.exit(0);
		if (!toggleSound.isActive())
		{
			game.getAudio().soundEnabled = false;
			Assets.music1.stop();
		}
		else if (toggleSound.isActive())
		{
			game.getAudio().soundEnabled = true;
			game.getAudio().playMusic(Assets.music1);
			Assets.music1.setLooping(true);
			Assets.music1.setVolume(1.0f);
		}
		if (buttonStart.wasPressed())
			game.setScreen(new MapScreen(game));
		else if (buttonExtra.wasPressed())
		game.setScreen(new ScoreScreen(game));
		else if (buttonInfo.wasPressed())
			game.setScreen(new InfoScreen(game));
	}

	@Override
	public void present(long delta)
	{
		spriteManager.beginDraw(Assets.tex_backgroundScreenRedMain);
		spriteManager.drawSprite(0, 0, width, height, Assets.background);
		spriteManager.endDraw();
		spriteManager.beginDraw(Assets.tex_splash2);
		spriteManager.drawSprite(width / 2 - 200 * multiplier / 2, height / 2 - (97 * multiplier / 2 - 50 * multiplier), 200 * multiplier, 97 * multiplier,
				Assets.splash2);
		spriteManager.endDraw();

		buttonStart.draw(game.getGraphics());
		buttonQuit.draw(game.getGraphics());
		buttonExtra.draw(game.getGraphics());
		buttonInfo.draw(game.getGraphics());
		toggleSound.draw(game.getGraphics());
	}

	@Override
	public void pause()
	{
	}

	@Override
	public void resume()
	{
		cam.setViewAndMatrices();

		game.getGraphics().initialize();

		game.getAudio().playMusic(Assets.music1);
		Assets.music1.setLooping(true);
		Assets.music1.setVolume(1.0f);

		width = game.getGraphics().getWidth();
		height = game.getGraphics().getHeight();
		multiplier = game.getGraphics().getMultiplier();
	}

	@Override
	public void dispose()
	{
	}

}
